/**
 * lilang
 * WfcCenterWorkAction.java
 * 2013-10-30下午6:52:49
 */
package com.cq.oa.web.wfc.action;

import java.util.ArrayList;
import java.util.List;

import oa.base.web.Action.BaseAction;

import com.cq.oa.domain.dto.InstanceDto;
import com.cq.oa.domain.dto.TaskCmd;
import com.cq.oa.domain.dto.WorkDto;
import com.cq.oa.domain.wfc.Activity;
import com.cq.oa.domain.wfc.ActivityInstance;
import com.cq.oa.domain.wfc.ProcessDefinition;
import com.cq.oa.domain.wfc.ProcessInstance;
import com.cq.oa.domain.wfc.TaskInstance;
import com.cq.oa.service.work.ProcessInstanceService;
import com.cq.oa.service.work.WorkCenterService;

/**
 * @author lilang
 *
 */
public class WfcCenterWorkAction extends BaseAction<ProcessDefinition>{
	
	private InstanceDto dto;
	
	private WorkCenterService workCenterService;
	
	
	private String url;
	
	
	
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	
	public InstanceDto getDto() {
		return dto;
	}

	public void setDto(InstanceDto dto) {
		this.dto = dto;
	}

	public void setWorkCenterService(WorkCenterService workCenterService) {
		this.workCenterService = workCenterService;
	}

	/**
	 * lilang
	 * 2013-10-30下午6:53:27
	 */
	private static final long serialVersionUID = -5150516962323284038L;

	/* (non-Javadoc)
	 * @see oa.base.web.Action.BaseAction#getEntityClass()
	 */
	@Override
	protected Class<ProcessDefinition> getEntityClass() {
		return ProcessDefinition.class;
	}

	/**
	 * 启动流程
	 * @return
	 * lilang 
	 * 2013-10-30下午6:55:01
	 */
	public String register(){
		//加载数据
		if(dto.getTaskId()!=null){
			loadProcessInstanceData(dto.getTaskId());
		}
		WorkDto workDto = this.workCenterService.getWorkProcessDefinition(dto.getSid());
		Activity activity = null;
		if(dto.getActivityId()!=null){
			activity = workDto.getNode(dto.getActivityId());
		}else{
			activity = workDto.getStartNode();
		}
		url= activity.getUrl();
		return SUCCESS;
	}
	
	/**
	 * 加载流程 ID 等数据
	 * @param taskId
	 * lilang 
	 * 2013-11-1上午10:49:08
	 */
	public void loadProcessInstanceData(Long taskId){
		ProcessInstanceService instanceService = this.workCenterService.getProcessInstanceService();
		TaskInstance instance = instanceService.getTaskInstanceDao().get(taskId);
		ActivityInstance activityInstance=instanceService.getActivityInstance(instance.getProcessActivitySid());
		ProcessInstance processInstance = instanceService.findByParmkeys(instance.getProcessInstanceSid());
		dto.setTaskId(taskId);
		dto.setProcessInstanceId(instance.getProcessInstanceSid());
		dto.setBusinessId(instance.getBusinessId());
		dto.setActivityInstanceId(instance.getProcessActivitySid());
		dto.setSid(instance.getProcessDefinitionSid());
		dto.setType(processInstance.getTypes());
		dto.setActivityId(activityInstance.getActivitySid());
		dto.setTitle(instance.getTitle());
	}
	/**
	 * 任务完成处理
	 * @return
	 * lilang 
	 * 2013-11-1下午2:32:24
	 */
	public String completeTask(){
		List<TaskCmd> cmd= new ArrayList<TaskCmd>();
		for(String taskOp:dto.getNextTaskOpers()){
				if("@@@".equals(taskOp)){
					break;
				}
				String[] op = taskOp.split("@");
				if(op.length<3){
					continue;
				}
				TaskCmd taskCmd = new TaskCmd();
				taskCmd.setTaskOper(Long.valueOf(op[1]));
				taskCmd.setTaskCname(op[2]);
				cmd.add(taskCmd);
		}
		try {
			this.workCenterService.doCompleteTask(dto.getTaskId(),cmd, dto.getActivityId());
			this.renderJsonSuccess();
		} catch (Exception e) {
			e.printStackTrace();
			this.renderJsonError(e);
		}
		return JSON;
	}
	
	/**
	 * 保存数据
	 * @return
	 * lilang 
	 * 2013-10-30下午7:02:38
	 * @throws Exception 
	 */
	public String saveData() throws Exception{
		//获取用户输入信息
		String msg = dto.getMsg();
		//没有流程实例启动流程
		if(dto.getProcessInstanceId()==null){
			dto = this.workCenterService.doStartWork(dto.getSid(), dto.getTitle());
		}
		this.workCenterService.updateTaskMsg(dto.getTaskId(), msg);
		this.renderJsonSuccess(jsonUtils.getExtJson(dto.getResultMap(), new String[]{}));
		return JSON;
	}
	
	/**
	 *  清楚流程
	 * @return
	 * lilang 
	 * 2013-11-1下午11:29:48
	 */
	public String clearTaskData(){
		TaskInstance instance = this.workCenterService.getProcessInstanceService().getTaskInstanceDao().get(dto.getTaskId());
		this.workCenterService.getProcessHisManger().doClearProcessInstance(instance.getBusinessId());
		this.renderJsonSuccess();
		return JSON;
	}
	
	public String process(){
		this.putKey.put("processList", this.workCenterService.getProcess());
		return "process";
	}
}
